package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.domain.User;
import com.itheima.entity.Result;
import com.itheima.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.itheima.service.BookService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class BookController {
    @Autowired
    private BookService bookservice;

    public ModelAndView selectnewbook(){
        int pageNum=1;
        int pageSize=5;
        PageResult pageresult = bookservice.selectnewbooks(pageNum, pageSize);//调用业务逻辑层方法，获取分页结果
        ModelAndView modelAndView=new ModelAndView();//创建ModelAndView对象
        modelAndView.addObject("pageresult",pageresult);//将分页结果传递给ModelAndView对象
        modelAndView.setViewName("newbook");//设置视图名
        return modelAndView;//返回ModelAndView对象
    }

    @ResponseBody
    @RequestMapping("findById")
    public Result<Book> findByid(String id){
        try{
            Book book = bookservice.findByid(id);
            if(book==null){
                return new Result<>(false,"查询失败，没有找到该书籍!");
            }
            return new Result<>(true,"查询成功!",book);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result<>(false,"查询失败!");
    }

    public Result borrowerbook(Book book, HttpSession session){
        String name = ((User) session.getAttribute("USER_SESSION")).getName();
        book.setBorrower(name);
        try{
            Integer count = bookservice.borrowerbook(book);
            if(count!=1){
                return new Result(false,"借阅失败!");
            }
            return  new Result(true,"借阅成功!,请到行政中心取书");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"借阅失败!");
        }
    }

    @RequestMapping("/search")
    public ModelAndView search(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request){
        if(pageNum==null){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        PageResult pageresult = bookservice.search(book, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();//创建ModelAndView对象
        modelAndView.setViewName("books");//设置视图名
        modelAndView.addObject("pageresult", pageresult);//将分页结果传递给ModelAndView对象
        modelAndView.addObject("search", book);//将搜索条件传递给ModelAndView对象
        modelAndView.addObject("pageNum", pageNum);//   将当前页数传递给ModelAndView对象
        modelAndView.addObject("gourl",request.getRequestURI());//   将当前页url传递给ModelAndView对象
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/addbook")
    public Result addbook(Book book){
        try{
            Integer count = bookservice.addbook(book);
            if(count!=1){
                return new Result(false,"添加失败!");
            }
            return new Result(true,"添加成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加失败!");
        }
    }

    @ResponseBody
    @RequestMapping("/editboook")
    public Result editbook(Book book){
        try{
            Integer count = bookservice.editbook(book);
            if(count!=1){
                return new Result(false,"编辑失败!");
            }
            return new Result(true,"编辑成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑失败!");
        }
    }

    @RequestMapping("/searchborrowed")
    public ModelAndView searchborrowed(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request){
        if(pageNum==null){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        User user = (User)request.getSession().getAttribute("USER_SESSION");
        PageResult pageresult = bookservice.searchborrowed(book, user, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();//创建ModelAndView对象
        modelAndView.setViewName("book_borrowed");//设置视图名
        modelAndView.addObject("pageresult", pageresult);//将分页结果传递给ModelAndView对象
        modelAndView.addObject("search", book);//将搜索条件传递给ModelAndView对象
        modelAndView.addObject("pageNum", pageNum);//   将当前页数传递给ModelAndView对象
        modelAndView.addObject("gourl",request.getRequestURI());//   将当前页url传递给ModelAndView对象
        return modelAndView;//返回ModelAndView对象
    }

    @ResponseBody
    @RequestMapping("/returnbook")
    public Result returnbook(String id, HttpSession session){
        User user = (User)session.getAttribute("USER_SESSION");
        try{
            boolean returnbook = bookservice.returnbook(id, user);
            if(!returnbook){
                return new Result(false,"归还失败!");
            }
            return new Result(true,"归还成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"归还失败!");
        }
    }

    @ResponseBody
    @RequestMapping("/returnconfirm")
    public Result returnconfirm(String id){
        try{
            Integer count = bookservice.returnconfirm(id);
            if(count!=1){
                return new Result(false,"确认归还失败!");
            }
            return new Result(true,"确认归还成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"确认归还失败!");
        }
    }
}
